﻿using System;
using System.Collections.Generic;
using Beisen.Data;
using fKanban.Model;
using fKanban.Utility.ObjectExtend;

namespace fKanban.Provider
{
    public class SprintProvider
    {
        public int Add(SprintEntity e)
        {
            Database db = Database.GetDatabase("fkanban");
            var identity = SafeProcedure.ExecuteScalar(db, "dbo.jSprintAdd", delegate(IParameterSet parameters)
            {
                parameters.AddWithValue("@UserStoryId", e.UserStoryId);

                parameters.AddWithValue("@Name", e.Name);
                parameters.AddWithValue("@StartDate", e.StartDate);
                parameters.AddWithValue("@EndDate", e.EndDate);
                parameters.AddWithValue("@Description", e.Description);
                
                parameters.AddWithValue("@ScrumMaster", e.ScrumMaster);
                parameters.AddWithValue("@TestManager", e.TestManager);
                parameters.AddWithValue("@Status", e.Status);
            });
            return identity.ToInt();
        }
        public int Update(SprintEntity e)
        {
            Database db = Database.GetDatabase("fkanban");
            return SafeProcedure.ExecuteNonQuery(db, "dbo.jSprintUpdateByPara",
            delegate(IParameterSet parameters)
            {
                parameters.AddWithValue("@ID", e.Id);
                parameters.AddWithValue("@UserStoryId", e.UserStoryId);
                parameters.AddWithValue("@Name", e.Name);
                parameters.AddWithValue("@Description", e.Description);
                parameters.AddWithValue("@StartDate", e.StartDate);
                parameters.AddWithValue("@EndDate", e.EndDate);

                parameters.AddWithValue("@ScrumMaster", e.ScrumMaster);
                parameters.AddWithValue("@TestManager", e.TestManager);
                parameters.AddWithValue("@Status", e.Status);
            });
        }
        public int Remove(SprintEntity e)
        {
            Database db = Database.GetDatabase("fkanban");
            return SafeProcedure.ExecuteNonQuery(db, "dbo.jSprintRemoveByPara",
            delegate(IParameterSet parameters)
            {
                parameters.AddWithValue("@ID", e.Id);
            });
        }
        public List<SprintEntity> LoadData()
        {
            Database db = Database.GetDatabase("fkanban");
            return SafeProcedure.ExecuteAndGetInstanceList<SprintEntity>(db, "dbo.jSprintGetALL",
            delegate(IParameterSet parameters)
            {
                //
            },
            delegate(IRecord record, SprintEntity objectInstance)
            {
                objectInstance.Id = record.GetOrDefault<int>("Id", 0);
                objectInstance.UserStoryId = record.GetOrDefault<int>("UserStoryId", 0);

                objectInstance.Name = record.GetOrDefault<string>("Name", string.Empty);
                objectInstance.Description = record.GetOrDefault<string>("Description", string.Empty);
                objectInstance.StartDate = record.GetOrDefault<DateTime>("StartDate", DateTime.MinValue);
                objectInstance.EndDate = record.GetOrDefault<DateTime>("EndDate", DateTime.MinValue);

                objectInstance.ScrumMaster = record.GetOrDefault<string>("ScrumMaster", string.Empty);
                objectInstance.TestManager = record.GetOrDefault<string>("TestManager", string.Empty);
                objectInstance.Status = record.GetOrDefault<int>("Status", 0);
            });
        }
        public List<SprintEntity> LoadData(SprintEntity e)
        {
            Database db = Database.GetDatabase("fkanban");
            return SafeProcedure.ExecuteAndGetInstanceList<SprintEntity>(db, "dbo.jSprintGetListByPara",
            delegate(IParameterSet parameters)
            {
                parameters.AddWithValue("@Id", e.Id);
                parameters.AddWithValue("@UserStoryId", e.UserStoryId);
                parameters.AddWithValue("@Name", e.Name);
                //parameters.AddWithValue("@Description", e.Description);
                //parameters.AddWithValue("@StartDate", e.StartDate);
                //parameters.AddWithValue("@EndDate", e.EndDate);

                parameters.AddWithValue("@ScrumMaster", e.ScrumMaster);
                parameters.AddWithValue("@TestManager", e.TestManager);
                parameters.AddWithValue("@Status", e.Status);
            },
            delegate(IRecord record, SprintEntity objectInstance)
            {
                objectInstance.Id = record.GetOrDefault<int>("Id", 0);
                objectInstance.UserStoryId = record.GetOrDefault<int>("UserStoryId", 0);

                objectInstance.Name = record.GetOrDefault<string>("Name", string.Empty);
                objectInstance.Description = record.GetOrDefault<string>("Description", string.Empty);
                objectInstance.StartDate = record.GetOrDefault<DateTime>("StartDate", DateTime.MinValue);
                objectInstance.EndDate = record.GetOrDefault<DateTime>("EndDate", DateTime.MinValue);

                objectInstance.ProductManager = record.GetOrDefault<string>("ProductManager", string.Empty);
                objectInstance.ScrumMaster = record.GetOrDefault<string>("ScrumMaster", string.Empty);
                objectInstance.TestManager = record.GetOrDefault<string>("TestManager", string.Empty);
                objectInstance.Status = record.GetOrDefault<int>("Status", 0);
            });
        }
        public SprintEntity LoadDataById(SprintEntity e)
        {
            var list = LoadData(e);
            if (null != list && list.Count > 0)
                return list[0];
            return null;
        }
    }
}